<?php
//节点模块
class NodeAction extends CommonAction {
	public function _filter(&$map)
	{
		
		$model	=	M("Node");
		$list	=	$model->where('level=2 and status=1')->select();
		/**if(!empty($_GET['group_id'])) {
		 $map['group_id'] =  $_GET['group_id'];
		 $this->assign('nodeName','分组');
		 }elseif(empty($_POST['search']) && !isset($map['pid']) ) {
			$map['pid']	=	0;
			}
			if($_GET['pid']!=''){
			$map['pid']=$_GET['pid'];
			}**/
		$map['group_id']  = array('gt',0);

		//$_SESSION['currentNodeId']	=	$map['pid'];
		$this->assign('level',3);
		//获取上级节点
		/**$node  = M("Node");
		 if(isset($map['pid'])) {
		 if($node->getById($map['pid'])) {
		 $this->assign('level',$node->level+1);
		 $this->assign('nodeName',$node->name);
		 }else {
		 $this->assign('level',2);
		 }
		 }**/
		$add=Array('add','foreverdelete','edit');//需要检测的操作是否有权限
		$add=$this->CheckAccess($add);
		$this->assign('opt',$add);
	}
	public function _before_index() {
		$model	=	M("Group");
		$list	=	$model->where('status=1')->getField('id,title');
		$this->assign('groupList',$list);
	}

	// 获取配置类型
	public function _before_add() {

		$model	=	M("Group");
		$list	=	$model->where('status=1')->select();
		$this->assign('list',$list);
		//$node	=	M("Node");
		//$node->getById($_SESSION['currentNodeId']);
		$this->assign('pid',1);//这里添加的都是模块 所以pid都是1
		$this->assign('level',2);//$node->level+1
	}

	public function _before_patch() {
		$model	=	M("Group");
		$list	=	$model->where('status=1')->select();
		$this->assign('list',$list);
		$node	=	M("Node");
		$node->getById($_SESSION['currentNodeId']);
		$this->assign('pid',$node->id);
		$this->assign('level',$node->level+1);
	}
	public function _before_edit() {
		$model	=	M("Group");
		$list	=	$model->where('status=1')->select();
		$this->assign('list',$list);
		$this->assign('level',2);
	}

	/**
	 +----------------------------------------------------------
	 * 默认排序操作
	 +----------------------------------------------------------
	 * @access public
	 +----------------------------------------------------------
	 * @return void
	 +----------------------------------------------------------
	 */
	public function sort()
	{
		$node = M('Node');
		if(!empty($_GET['sortId'])) {
			$map = array();
			$map['status'] = 1;
			$map['id']   = array('in',$_GET['sortId']);
			$sortList   =   $node->where($map)->order('sort asc')->select();
		}else{
			if(!empty($_GET['pid'])) {
				$pid  = $_GET['pid'];
			}else {
				$pid  = $_SESSION['currentNodeId'];
			}
			if($node->getById($pid)) {
				$level   =  $node->level+1;
			}else {
				$level   =  1;
			}
			$this->assign('level',$level);
			$sortList   =   $node->where('status=1 and pid='.$pid.' and level='.$level)->order('sort asc')->select();
		}
		$this->assign("sortList",$sortList);
		$this->display();
		return ;
	}

	function Excelout(){
		$model	=	M("Node");
		$list	=	$model->where('level=2 and status=1')->select();
		
		import ( "ORG.Util.PHPExcel.PHPExcel" ); 
	    import ( "ORG.Util.PHPExcel.PHPExcel_Writer_Excel5" ); 

	    // 创建一个处理对象实例
		$objExcel = new PHPExcel();
		// 创建文件格式写入对象实例, uncomment
		$objWriter = new PHPExcel_Writer_Excel5($objExcel);
	    //设置当前活动sheet的名称       
	    //缺省情况下，PHPExcel会自动创建第一个sheet被设置SheetIndex=0       
		$objExcel->setActiveSheetIndex(0);       
		$objActSheet = $objExcel->getActiveSheet();  
		$objActSheet->setTitle('当前sheetname');
		
		//设置表格标题栏内容
		$objActSheet->setCellValue('A1', '编号')->getStyle('A1')->getFont()->setBold(true);    
		$objActSheet->setCellValue('B1', '名称')->getStyle('B1')->getFont()->setBold(true);       
		$objActSheet->setCellValue('C1', '显示名')->getStyle('C1')->getFont()->setBold(true); 
		$objActSheet->getColumnDimension('C')->setWidth(50);    
		$n=2;
		foreach ($list as $k=>$v){
			$objActSheet->setCellValue('A'.$n, $v['id']);
			$objActSheet->setCellValue('B'.$n, $v['name']);
			$objActSheet->setCellValue('C'.$n, $v['title']);
			$n++;
		}
		
		//输出内容       
		$outputFileName =C('EXCEL_FILE_PATH')."/temp.xls";
	    $objWriter->save($outputFileName); 
		$data=Array('path'=>$outputFileName,'name'=>'temp.xls');
		$this->ajaxReturn($data,"tttt",1);
		//$this->display ();
	}

}